package houseSystem.view;

import com.houseSystem.domain.house;
import com.houseSystem.service.houseService;
import com.houseSystem.utils.Utility;

public class houseView {

    private boolean loop = true;
    private char key;
    private houseService h = new houseService(10);

    public void mainMenu() {
        do {
            System.out.println("-------------房屋出租系统-------------");
            System.out.println("           1. 新 增 房 屋");
            System.out.println("           2. 查 找 房 屋");
            System.out.println("           3. 删 除 房 屋");
            System.out.println("           4. 修 改 房 屋 信 息");
            System.out.println("           5. 房 屋 列 表");
            System.out.println("           6. 退       出");
            System.out.println("请输入你的选择(1-6)");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    System.out.println("-------------添加房屋-------------");
                    addHouse();
                    break;
                case '2':
                    System.out.println("-------------查找房屋-------------");
                    findHouse();
                    break;
                case '3':
                    System.out.println("-------------删除房屋-------------");
                    deleteHouse();
                    break;
                case '4':
                    System.out.println("-------------修改信息-------------");
                    modifyHouse();
                    break;
                case '5':
                    System.out.println("-------------房屋列表-------------");
                    listHouses();
                    break;
                case '6':
                    System.out.println("-------------退----出-------------");
                    if (Utility.readConfirmSelection() == 'N') {
                        break;
                    }
                    loop = false;
                    break;
            }
        } while (loop);
    }

    public void listHouses() {
        System.out.println("编号" + "\t" + "房主" + "\t" + "电话" + "\t" + "地址" + "\t" + "月租" + "\t" + "状态(未出租/已出租)");
        house[] houses = h.list();
        for (int i = 0; houses[i] != null; i++) {
            System.out.println(houses[i]);
        }
        System.out.println("---------房屋列表显示完毕----------");
    }

    public void addHouse() {
        System.out.print("姓名: ");
        String name = Utility.readString(8);
        System.out.print("电话: ");
        String phone = Utility.readString(12);
        System.out.print("地址: ");
        String address = Utility.readString(16);
        System.out.print("月租: ");
        int rent = Utility.readInt();
        System.out.print("状态: ");
        String state = Utility.readString(3);
        house newHouse = new house(0, name, phone, address, rent, state);
        h.add(newHouse);
        System.out.println("-------------添加完成-------------");
    }

    public void deleteHouse() {
        System.out.println("请输入要删除的id号(-1退出)");
        int id = Utility.readInt();
        if (id == -1) {
            System.out.println("-------------你放弃了删除-------------");
            return;
        }
        if (Utility.readConfirmSelection() == 'N') {
            System.out.println("-------------你放弃了删除-------------");
            return;
        }
        if (h.delete(id)) {
            System.out.println("-------------删除完成-------------");
        } else {
            System.out.println("-------------删除失败-------------");
        }
    }

    public void findHouse() {
        System.out.println("---------请输入你要查找的id---------");
        int id = Utility.readInt();
        if (h.find(id) == null) {
            System.out.println("-------------查找失败-------------");
        } else {
            System.out.println("-------------查找成功-------------");
            System.out.println("编号" + "\t" + "房主" + "\t" + "电话" + "\t" + "地址" + "\t" + "月租" + "\t" + "状态(未出租/已出租)");
            System.out.println(h.find(id).toString());
        }
    }

    public void modifyHouse() {
        System.out.println("请输入你要修改的id(-1退出)");
        int id = Utility.readInt();
        if (id == -1) {
            return;
        }
        house home = h.find(id);
        if (home == null) {
            System.out.println("-------------修改的信息不存在-------------");
            return;
        }
        System.out.println("姓名: " + home.getName());
        String name = Utility.readString(8, "");
        if (!"".equals(name)) {
            home.setName(name);
        }
        System.out.println("电话: " + home.getPhone());
        String phone = Utility.readString(8, "");
        if (!"".equals(phone)) {
            home.setPhone(phone);
        }
        System.out.println("地址: " + home.getAddrss());
        String address = Utility.readString(8, "");
        if (!"".equals(address)) {
            home.setAddrss(address);
        }
        System.out.println("月租: " + home.getRent());
        int rent = Utility.readInt(-1);
        if (rent != -1) {
            home.setRent(rent);
        }
        System.out.println("状态: " + home.getState());
        String state = Utility.readString(8, "");
        if (!"".equals(state)) {
            home.setState(state);
        }
    }
}
